define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config) {

            // 流程
            $scope.phases = ['process_tool'];

            $scope.init = function(){
                $scope.user_name = config.cache.name;
                $scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);
                $scope.item_list = [];
                $scope.mode = ['how_info'];
                // 預設流程狀態
                $scope.phases_index = 0;
                $scope.reason_index = '2';
                $scope.abnormal_list = [];

                // 自動隱蔽鍵盤
                element_barcode_no = document.getElementById('input_barcode');
                makeBarcodeInput(element_barcode_no);
                element_barcode_no.focus();
            }

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('menu', true);
            };

            $scope.changeTab = function(num){
                $scope.reason_index = num;
            }

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        input_barcode_no_callback(result.text);
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            // barcode輸入
            $scope.input_barcode = function(barcode){
                input_barcode_no_callback(barcode);
            };

            // 流程狀態
            $scope.PhaseStatus = function(index){
                if($scope.phases_index > index)
                    return ['prev'];
                else if($scope.phases_index == index)
                    return ['now'];
                else
                    return ['next'];
            };

            // 回上一步流程
            $scope.BackToPhase = function (index){
                if($scope.phases_index > index){
                    $scope.phases_index = index;
                    if(index == 0){
                        $scope.tool_class = '';
                        $scope.tool_no = '';
                        $scope.tool_state = '';
                        $scope.last_fix_date = '';
                        $scope.sendRepair_reason = '';
                        $scope.life_count = '';
                        $scope.acc_count = '';
                        $scope.usage_count = '';
                        $scope.actual_count = '';
                        $scope.reason_index = '2';

                        $scope.mode = ['how_info'];

                        $scope.item_list = [];
                        $scope.reason_desc = '';
                        $scope.maintain_reason_list = [];
                        $scope.phases = ['process_tool'];
                    };
                };
            };

            // barcode輸入 callback
            function input_barcode_no_callback(barcode){
                if(barcode != ''){
                    // 工具流程
                    if($scope.phases_index == 0){
                        var content = {
                            tool_no: barcode
                        };
                        tool_info_get(content,function(data){
                            console.log('data',data)
                            // 送修 70
                            if(data.state != '70'){
                                // 已上料 40 || 已上線 60 || 報廢 80
                                if(data.state == '40' || data.state == '60' || data.state == '80'){
                                    $scope.$root.showAlert($filter('translate')('SMT12.msg.this_tool_cannot_sendRepair', {state:$filter('translate')('SMT.common.state.s_'+data.state)}));
                                } else {
                                    // 未送修 - 送修原因流程
                                    $scope.mode = ['sendRepair_reason'];
                                    $scope.phases_index = 1;
                                    $scope.phases = ['process_tool', 'process_sendRepair_reason'];
                                    $scope.tool_type_no = data.tool_type_no;
                                    $scope.tool_class = data.tool_class;
                                    $scope.tool_no = data.tool_no;
                                    $scope.tool_state = data.state;
                                    $scope.last_fix_date = data.last_fix_date;
                                    $scope.life_count = data.life_count;
                                    $scope.acc_count = data.acc_count;
                                    $scope.usage_count = data.usage_count;
                                    $scope.actual_count = data.actual_count;
                                };
                            } else {
                                if(data.state != '70'){
                                    $scope.$root.showAlert($filter('translate')('SMT12.msg.this_tool_cannot_repair', {state:$filter('translate')('SMT.common.state.s_'+data.state)}));
                                } else {
                                    // 送修中 - 維修完成流程
                                    $scope.mode = ['repair_completed'];
                                    $scope.phases_index = 1;
                                    $scope.phases = ['process_tool', 'process_repair_completed'];
                                    $scope.tool_type_no = data.tool_type_no;
                                    $scope.tool_class = data.tool_class;
                                    $scope.tool_no = data.tool_no;
                                    $scope.tool_state = data.state;
                                    $scope.sendRepair_date_done = data.last_fix_date;
                                    $scope.reason_desc = '';
                                    if(data.maintain_reason_list && data.maintain_reason_list.length > 0){
                                        data.maintain_reason_list.forEach(function(reason_item){
                                            if($scope.reason_desc != '')
                                                $scope.reason_desc += ', ';
                                            $scope.reason_desc += reason_item.reason_name;
                                        });
                                    }
                                    $scope.maintain_no = data.maintain_no;
                                    $scope.maintain_reason_list = data.maintain_reason_list;
                                };
                            };
                        });
                    } else if($scope.phases_index == 1){
                        if($scope.phases = ['process_tool', 'process_sendRepair_reason']){
                            tool_abnormal_reason_info_get(barcode, function(itemList){
                                if(itemList && itemList.length > 0){
                                    $scope.item_list.push(
                                        {
                                            reason_no: itemList[0].reason_no,
                                            reason_name: itemList[0].reason_name,
                                            memo: itemList[0].memo
                                        }
                                    );
                                } else {
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                                };
                            });
                        }
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    };
                };
            };

            // 開窗選擇送修原因
            $scope.AddReason = function () {
                if($scope.phases_index == 1){
                    $scope.$root.showQueryList({
                        key: 'reason_no',
                        header: [
                            {label:$filter('translate')('SMT12.list_repair.reason_no'), code:'reason_no'},
                            {label:$filter('translate')('SMT12.list_repair.reason_name'), code:'reason_name'}
                        ],
                        query: function(conditions, feedback){
                            tool_abnormal_reason_info_get('', function(itemList){
                                console.log('itemList',itemList);
                                if(itemList && itemList.length > 0){
                                    feedback(itemList);
                                } else {
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                                };
                            });
                        },
                        confirm:function(selectItem, dialog){
                            dialog.hide();
                            var _temp = $filter('filter')($scope.item_list, {reason:selectItem.reason_no});
                            if(_temp && _temp.length == 0)
                                $scope.item_list.push({reason_no:selectItem.reason_no, reason_name:selectItem.reason_name, memo:''});
                        }
                    });
                }else{
                    $scope.$root.showAlert()
                }
            };

            // 刪除送修原因項目
            $scope.RemoveReason = function (item) {
                $scope.item_list.splice($scope.item_list.indexOf(item), 1);
            };

            $scope.LoadReason = function(){
                $scope.$root.showQueryList({
                    key: 'reason_no',
                    header: [
                        {label:$filter('translate')('SMT12.list_repair.reason_no'), code:'reason_no'},
                        {label:$filter('translate')('SMT12.list_repair.reason_name'), code:'reason_name'}
                    ],
                    query: function(conditions, feedback){
                        feedback($scope.maintain_reason_list);
                    },
                    confirm:function(selectItem, dialog){
                        // $scope.reason_desc = selectItem.reason_name;
                        dialog.hide();
                        $scope.abnormal_list.push({abnormal_no:selectItem.reason_no, abnormal_name:selectItem.reason_name});
                    }
                });
            };

            // 點擊確認送出資料
            $scope.confirm = function(){
                if($scope.phases_index == 1){
                    if($scope.item_list&& $scope.item_list.length > 0){
                        var content = {
                            tool_type_no: $scope.tool_type_no,
                            tool_no: $scope.tool_no,
                            tool_class: $scope.tool_class,
                            state: $scope.tool_state,
                            maintain_no:$scope.maintain_no,
                            reason_list:$scope.item_list
                        };
                        if($scope.mode.indexOf('repair_completed')!= -1)
                            content.state = '70';

                        tool_info_set_process(content, function(data){
                            $scope.$root.showToast($filter('translate')('SMT12.msg.sendRepair_success'));
                            $scope.BackToPhase(0);
                        });
                    } else {
                        $scope.$root.showAlert($filter('translate')('SMT12.msg.reason_list_empty'));
                    }
                } else {
                    $scope.$root.showAlert($filter('translate')('SMT12.msg.tool_empty'));
                }
            };

            // 取回 工具資訊
            function tool_info_get(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.tool_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };


            // 取得 工具異常原因
            function tool_abnormal_reason_info_get(reason_no, callback){
                var query_condition_info = [];
                if(reason_no && reason_no != '')
                    query_condition_info.push({column_name:'ReasonNo', query_mode:'0', value: reason_no, merge_condition_model:'AND'});

                if($scope.mode.indexOf('repair_completed')!= -1)
                    query_condition_info.push({column_name:'ReasonType', query_mode:'0', value: '3', merge_condition_model:'AND'});
                else
                    query_condition_info.push({column_name:'ReasonType', query_mode:'0', value: '2', merge_condition_model:'AND'});

                $MMWService.pageNumberInfoGet({
                    page_mode : 'tool_abnormal_reason_info_get',
                    query_condition_info:query_condition_info
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            // 送回資料
            function tool_info_set_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.tool_info_set_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            $scope.input_PDA_barcode = function(event){
                if (event.keyCode == 13) {
                    element_barcode_no.blur();
                    input_barcode_no_callback($scope.barcode)
                    $scope.barcode = '';
                } else {
                    $scope.barcode = $scope.barcode || '';
                }
            }

            //20200914 modify by Dustdusk for M#: 增加
            function makeBarcodeInput(el) {
                var _input = el.getElementsByClassName('barcode_input')[0];
                //var _mask = el.getElementsByClassName('barcode_mask')[0];
                el.focus = function () {
                    //e.preventDefault();
                    //_mask.click();
                    el.click();
                }
                el.addEventListener('click', function (e) {
                    e.preventDefault();
                    //_input.value = '';
                    if (document.activeElement !== _input)
                        _input.focus();
                });
                _input.addEventListener('focus', function (e) {
                    e.preventDefault();
                    setTimeout(function () {
                        _input.removeAttribute('readonly');
                    }, 200);
                });
                _input.addEventListener('blur', function (e) {
                    e.preventDefault();
                    _input.setAttribute('readonly', 'readonly');
                });
            }
        }
    ];
});
